﻿Ext.namespace("Swfu");
Ext.namespace("Swfu.BackMgr");

//日期转换,val形如:/Date(1309104000000)/
function DateParse(val) { return eval("new " + val.substr(1, val.length - 2)).format('Y-m-d H:i:s') }
function DateParseWithoutTime(val) { return eval("new " + val.substr(1, val.length - 2)).format('Y-m-d') }
function parseHM(val, a, reader) {
    if (val!=undefined && val!= "") {
        var t = val.split(' ')[1];
        var retStr = t.substr(0, t.lastIndexOf(":"));
        var tInt = parseInt(retStr.replace(":", ""));
        if (reader.data.CardType == 'am') {
            if (830 - tInt < 0) {
                retStr = "<font color='red'>"+retStr + "</font>";
            }
        }
        else {
            if (tInt - 1800 < 0) {
                retStr = "<font color='red'>" + retStr + "</font>";
            } 
        }
        return retStr;
    }
}

//取get参数
function getArg() {
    var url = unescape(window.location.href);
    var allArgs = url.split("?")[1];
    if (allArgs != null) {
        var args = allArgs.split("&");
        for (var i = 0; i < args.length; i++) {
            var arg = args[i].split("=");
            eval('this.' + arg[0] + '="' + arg[1] + '";');
        }
    }
}

//签核表单状态
function getStatusName(value) {
    var statusName;
    switch (value) {
        case "0": statusName = "保存"; break;
        case "1": statusName = "提交"; break;
        case "2": statusName = "通过"; break;
        case "3": statusName = "退件"; break;
        case "4": statusName = "待销单"; break;
        case "5": statusName = "销单通过"; break;
        case "6": statusName = "销单退件"; break;
        default: statusName = "异常";
    }
    return statusName;
}
function getAuthedLinks(value, a, reader, c, d, ds) {
    var links = [];
    var hasSign = reader.data.StatusId != "0";
    links.push('<a href="#" onclick="return SignWin(\'' + value + '\',true,' + hasSign + ');">查看</a>');
    if (reader.data.EmpId == Cookies.get("empId") && "0,3".indexOf(reader.data.StatusId) >= 0) {
        links.push('<a href="#" onclick="return SignWin(\'' + value + '\',false,false);">编辑</a>');
    }
    if (reader.data.StatusId == "1" && reader.data.CheckEmpId == Cookies.get("empId")) {
        links.push('<a href="#" onclick="return SignWin(\'' + value + '\',true,true);">签核</a>');
    }
    if (reader.data.EmpId == Cookies.get("empId") && "0,3".indexOf(reader.data.StatusId) >= 0) {
        links.push('<a href="#" onclick="return delForm(\'' + value + '\');">删除</a>');
    }
    //销单申请,排除不需要销单和销单不用审批的单据
    if (reader.data.StatusId == "2" && "2".indexOf(Cookies.get("empId")) >= 0 && "Vehi".indexOf(value.substr(5, 4)) >= 0) {//用车单由司机销单
        links.push('<a href="#" onclick="return SignWin(\'' + value + '\',false,false);">销单</a>');
    } else if (reader.data.StatusId == "2" && reader.data.EmpId == Cookies.get("empId") && "Week,Vehi,Invi".indexOf(value.substr(5, 4)) == -1) {
        links.push('<a href="#" onclick="return SignWin(\'' + value + '\',false,false);">销单申请</a>');
    }
    if (reader.data.StatusId == "4" && reader.data.CheckEmpId == Cookies.get("empId")) {
        links.push('<a href="#" onclick="return SignWin(\'' + value + '\',true,true);">销单审批</a>');
    }
    return links.join("&nbsp;");
}
function delForm(fNo) {
    var url;
    switch (fNo.substr(0, 9)) {
        case "Form_Week":
            url = "/Weekly.mvc/Delete"; break;
        case "Form_Vehi":
            url = "/Vehicle.mvc/Delete"; break;
        case "Form_Evec":
            url = "/Evection.mvc/Delete"; break;
        case "Form_Over":
            url = "/Overtime.mvc/Delete"; break;
        case "Form_Vaca":
            url = "/Vacation.mvc/Delete"; break;
        case "Form_Meet":
            url = "/MeetingRoom.mvc/Delete"; break;
        case "Form_Perc":
            url = "/Perchase.mvc/Delete"; break;
        case "Form_Rece":
            url = "/Reception.mvc/Delete"; break;
        case "Form_Invi":
            url = "/Invite.mvc/Delete"; break;
        default:
            Ext.ux.Toast.msg('提示', '未找到该表单,请联系技术员修复!'); break;
    }
    Ext.Msg.show({
        title: '系统确认',
        msg: '你是否确定删除这些数据?',
        buttons: Ext.Msg.YESNO,
        scope: this,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: url, //调用谁?
                    success: function (response, options) {
                        var responseMessage = Ext.util.JSON.decode(response.responseText);
                        if (responseMessage.Result) {
                            RefreshList(fNo); //刷新谁?
                            Ext.ux.Toast.msg('提示', '已经成功删除选中的信息。');
                        }
                        else {
                            Ext.ux.Toast.msg('提示', '删除失败，请您重试。');
                        }
                    },
                    failure: function (action) {
                        if (action.failureType == 'server') {
                            obj = Ext.util.JSON.decode(action.response.responseText);
                            Ext.Msg.alert('错误:', obj.errors.reason);
                        }
                    },
                    params: {
                        formNo: fNo
                    }
                });
            }
        },
        icon: Ext.MessageBox.QUESTION
    });
}

function DataSizeChange(val) {
    if (val > 0) { val = Ext.util.Format.fileSize(val); }
    else { val = ""; }
    return val;
}

function DocUsedLog(docId, isEdit) {

    Ext.Ajax.request({
        url: '/File.mvc/AccessLog',
        method: 'POST',
        params: { fileId: docId },
        success: function (response, options) {
            var responseMessage = Ext.util.JSON.decode(response.responseText);
            if (responseMessage.Result) {
                if (Ext.getCmp("daiDuGrid") != null) { Ext.getCmp("daiDuGrid").getStore().reload(); }
                Ext.ux.Toast.msg('提示', '日志添加成功！');
            }
            else {
                Ext.ux.Toast.msg('错误', '日志添加失败，请您重试！');
            }
        },
        failure: function (form, action) {
            if (action.failureType == 'server') {
                obj = Ext.util.JSON.decode(action.response.responseText);
                Ext.ux.Toast.msg('错误', obj.errors.reaso);
            }
            if (action.failureType == 'client') {
                Ext.ux.Toast.msg('错误', '日志添加失败，请您重试');
            }
        }
    });
    return true;
}

function delDocNo(id) {
    Ext.Msg.show({
        title: '系统确认',
        msg: '你是否确定删除这些数据?',
        buttons: Ext.Msg.YESNO,
        scope: this,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '/DocNo.mvc/DeleteDoc',
                    success: function (response, options) {
                        var responseMessage = Ext.util.JSON.decode(response.responseText);
                        if (responseMessage.Result) {
                            if (Ext.getCmp("DocNoMgr") != null) { Ext.getCmp("DocNoMgr").getStore().reload(); }
                            Ext.ux.Toast.msg('提示', '已经成功删除数据。');
                        }
                        else {
                            Ext.ux.Toast.msg('提示', '删除失败，请您重试。');
                        }
                    },
                    failure: function (action) {
                        if (action.failureType == 'server') {
                            obj = Ext.util.JSON.decode(action.response.responseText);
                            Ext.Msg.alert('错误:', obj.errors.reason);
                        }
                    },
                    params: {
                        id: id
                    }
                });
            }
        }
    });
}